package com.example.service.impl;

import com.example.model.PurchaseOrderQueryDTO;
import com.example.model.PurchaseOrderVO;
import com.example.service.PurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 采购订单服务实现类
 */
@Service
public class PurchaseOrderServiceImpl implements PurchaseOrderService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<PurchaseOrderVO> queryPurchaseOrders(PurchaseOrderQueryDTO queryDTO) {
        StringBuilder sql = new StringBuilder();
        List<Object> params = new ArrayList<>();

        sql.append("SELECT ");
        sql.append("  po.id, po.code, po.create_time, ");
        sql.append("  s.name AS supplier_name, ");
        sql.append("  p.name AS product_name, ");
        sql.append("  p.spec, ");
        sql.append("  COALESCE(pod.unit, p.unit) AS unit, ");
        sql.append("  pod.tax_price, ");
        sql.append("  pod.order_num, ");
        sql.append("  pod.amount, ");
        sql.append("  COALESCE(pod.currency, po.currency) AS currency, ");
        sql.append("  CASE ");
        sql.append("    WHEN po.status = 0 THEN '草稿' ");
        sql.append("    WHEN po.status = 1 THEN '审核中' ");
        sql.append("    WHEN po.status = 2 THEN '审核通过' ");
        sql.append("    WHEN po.status = 3 THEN '审核拒绝' ");
        sql.append("    ELSE '未知' ");
        sql.append("  END AS status ");
        sql.append("FROM tbl_purchase_order po ");
        sql.append("JOIN tbl_purchase_order_detail pod ON po.id = pod.order_id ");
        sql.append("JOIN base_data_supplier s ON po.supplier_id = s.id ");
        sql.append("JOIN base_data_product p ON pod.product_id = p.id ");
        sql.append("WHERE 1=1 ");

        // 添加查询条件
        if (queryDTO != null) {
            if (queryDTO.getCode() != null && !queryDTO.getCode().isEmpty()) {
                sql.append("AND po.code LIKE ? ");
                params.add("%" + queryDTO.getCode() + "%");
            }
            
            if (queryDTO.getSupplierId() != null && !queryDTO.getSupplierId().isEmpty()) {
                sql.append("AND po.supplier_id = ? ");
                params.add(queryDTO.getSupplierId());
            }
            
            if (queryDTO.getProductId() != null && !queryDTO.getProductId().isEmpty()) {
                sql.append("AND pod.product_id = ? ");
                params.add(queryDTO.getProductId());
            }
            
            if (queryDTO.getStartDate() != null) {
                sql.append("AND po.create_time >= ? ");
                params.add(queryDTO.getStartDate());
            }
            
            if (queryDTO.getEndDate() != null) {
                sql.append("AND po.create_time <= ? ");
                params.add(queryDTO.getEndDate());
            }
            
            if (queryDTO.getStatus() != null) {
                sql.append("AND po.status = ? ");
                params.add(queryDTO.getStatus());
            }
        }
        
        sql.append("ORDER BY po.create_time DESC");

        List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql.toString(), params.toArray());
        List<PurchaseOrderVO> result = new ArrayList<>();
        
        for (Map<String, Object> row : rows) {
            PurchaseOrderVO vo = new PurchaseOrderVO();
            vo.setId((String) row.get("id"));
            vo.setCode((String) row.get("code"));
            vo.setCreateTime((java.util.Date) row.get("create_time"));
            vo.setSupplierName((String) row.get("supplier_name"));
            vo.setProductName((String) row.get("product_name"));
            vo.setSpec((String) row.get("spec"));
            vo.setUnit((String) row.get("unit"));
            vo.setTaxPrice((java.math.BigDecimal) row.get("tax_price"));
            vo.setOrderNum((Integer) row.get("order_num"));
            vo.setAmount((java.math.BigDecimal) row.get("amount"));
            vo.setCurrency((String) row.get("currency"));
            vo.setStatus((String) row.get("status"));
            
            result.add(vo);
        }
        
        return result;
    }
}